//
//  YXLeftMenuTableViewController.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/21.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXLeftMenuTableViewController.h"
#import "YXDrawerViewController.h"
#import "YXMineMenuView.h"
#import "YXMineWorkOrderViewController.h"
#import "YXMineAddressViewController.h"
#import "YXMineServiceViewController.h"
#import "YXMineSettingViewController.h"
#import "YXForumViewController.h"
#import "YXLoginAndRegistViewController.h"
@interface YXLeftMenuTableViewController ()
@property (nonatomic ,strong) UIView *headerView;
@property (nonatomic ,strong) UIButton *exitBtn;
@property (nonatomic ,strong) NSArray *imageArr;
@property (nonatomic ,strong) NSArray *titleArr;
@property (nonatomic ,strong) UIImageView *userImage;
@end

@implementation YXLeftMenuTableViewController

- (UIImageView *)userImage {
    if (!_userImage) {
        self.userImage = [[UIImageView alloc] initWithFrame:(CGRectMake(40, 75, 60, 60))];
        //    userImage.backgroundColor = [UIColor redColor];
        [ self.userImage sd_setImageWithURL:[NSURL URLWithString:[YXUserInfoManager getUserInfo].imgUrl] placeholderImage:[UIImage imageNamed:@"logo"]];
        self.userImage.layer.masksToBounds = YES;
        self.userImage.layer.cornerRadius =  self.userImage.W / 2;
    }
    return _userImage;
}

- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 280))];
        _headerView.backgroundColor = [UIColor whiteColor];
    }
    return _headerView;
}

- (UIButton *)exitBtn {
    if (!_exitBtn) {
        _exitBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _exitBtn.frame = CGRectMake(15, kScreenHeight - 15, 20, 20);
        [_exitBtn setImage:[UIImage imageNamed:@"023"] forState:(UIControlStateNormal)];
        [_exitBtn addTarget:self action:@selector(exitBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
  
    }
    return _exitBtn;
}

- (void)exitBtnAction {
    //提示框
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"退出后您将不能查看相关信息" message:@"确定退出吗" preferredStyle:(UIAlertControllerStyleAlert)];
    
    // 取消按钮
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        //取消的内容
    }];
    // 确定按钮
    UIAlertAction *sendAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

//        http://121.196.210.134:8082/mpos/wx/signOutLogin.do
        NSString *urlStr = @"signOutLogin.do";
        YXUserInfoModel *model = [YXUserInfoManager getUserInfo];
        NSDictionary *parameter = @{@"sessionId":model.sessionId};
        [BX_NetWorkManager requestForPOSTWithUrl:urlStr parameter:parameter success:^(id responseObject) {
            if (REQUESTDATASUCCESS) {
                //清空用户信息
                [YXUserInfoManager cleanUserInfo];
                
                //确定的内容
                YXLoginAndRegistViewController *loginVC = [[YXLoginAndRegistViewController alloc] init];
                loginVC.typeView = LogingView;
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
                UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
                rootWindow.rootViewController = nav;
            }
        } failure:^(NSError *error) {
            
        }];
        

        
    }];
    //将事件添加到提示框上
    [alertVC addAction:cancelAction];
    [alertVC addAction:sendAction];
    //弹出提示框(C层)
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

- (NSArray *)imageArr {
    if (!_imageArr) {
        _imageArr = @[@"028",@"026",@"025",@"024"];
    }
    return _imageArr;
}

- (NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[@"地址管理",@"联系客服",@"邀请好友",@"设置"];
    }
    return _titleArr;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    if ([YXUserInfoManager isLoad]) {
//        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"imgUrl"];
//        if (data != nil) {
//            self.userImage.image = [UIImage imageWithData:data];
//        } else {
//            self.userImage.image = [UIImage imageNamed:@"logo"];
//        }
//    }else {
//        self.userImage.image = [UIImage imageNamed:@"logo"];
//
//    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableViewUI];
    [self.view addSubview:self.exitBtn];
    
    // 注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imgUrlAction:) name:@"notificationImageUrl" object:nil];

}

- (void)imgUrlAction:(NSNotification *)noti {
//    NSLog(@"%@",noti);
    [self.userImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",noti.userInfo[@"imgUrl"]]]];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"notificationImageUrl" object:nil];
}

#pragma mark - 设置UI
- (void)createTableViewUI {
    self.tableView.frame = CGRectMake(0, -20, kScreenWidth, kScreenHeight+20);
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableHeaderView = self.headerView;
    
    UIView *backView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 170))];
    backView.backgroundColor = color_globalColor;
    [self.headerView addSubview:backView];
    
    YXUserInfoModel *model = [YXUserInfoManager getUserInfo];
//    UIImageView *userImage = [[UIImageView alloc] initWithFrame:(CGRectMake(40, 75, 60, 60))];
////    userImage.backgroundColor = [UIColor redColor];
//    [userImage sd_setImageWithURL:[NSURL URLWithString:[YXUserInfoManager getUserInfo].imgUrl] placeholderImage:[UIImage imageNamed:@""]];
//    userImage.layer.masksToBounds = YES;
//    userImage.layer.cornerRadius = userImage.W / 2;
//    [backView addSubview:userImage];
//    self.userImage = userImage;
    
    [backView addSubview: self.userImage];
    
    
    UILabel *nameLab = [[UILabel alloc] initWithFrame:(CGRectMake(115, 85, 200, 20))];
    nameLab.text = model.name;
    nameLab.textColor = [UIColor whiteColor];
    nameLab.font = [UIFont boldSystemFontOfSize:22];
    [backView addSubview:nameLab];
    
    UILabel *phoneLab = [UILabel setLabelWithText:model.phone fontSize:16 color:[UIColor whiteColor]];
    phoneLab.frame = CGRectMake(115, nameLab.H + nameLab.Y + 4, 150, 20);
    [backView addSubview:phoneLab];
    
    YXMineMenuView *menuView = [[YXMineMenuView alloc] initWithFrame:(CGRectMake(0,backView.H , 300, 70))];
    menuView.layer.shadowColor = [UIColor blackColor].CGColor;
    menuView.layer.shadowOffset = CGSizeMake(-3, 3);
    menuView.layer.shadowOpacity = 0.3;
    menuView.layer.shadowRadius = 5;
    [self.headerView addSubview:menuView];
    
    [menuView setClickMenuBtnBlock:^(NSInteger index) {
        NSLog(@"%ld",index);
        if (index == 0) {
            YXMineWorkOrderViewController *workVC = [[YXMineWorkOrderViewController alloc] init];
            workVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"导航栏_002"] style:(UIBarButtonItemStyleDone) target:[YXDrawerViewController shareDrawer] action:@selector(backHome)];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:workVC];
            [[YXDrawerViewController shareDrawer] switchViewController:nav];

        }else {
            YXForumViewController *forumVC = [[YXForumViewController alloc] init];
            forumVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"导航栏_002"] style:(UIBarButtonItemStyleDone) target:[YXDrawerViewController shareDrawer] action:@selector(backHome)];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:forumVC];
            
            [[YXDrawerViewController shareDrawer] switchViewController:nav];
        }
    }];
    
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"mineCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.imageArr[indexPath.row]]];
    cell.textLabel.text = self.titleArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        YXMineAddressViewController *addressVC = [[YXMineAddressViewController alloc] init];
        addressVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"导航栏_002"] style:(UIBarButtonItemStyleDone) target:[YXDrawerViewController shareDrawer] action:@selector(backHome)];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:addressVC];
        
        // 切换控制器
        [[YXDrawerViewController shareDrawer] switchViewController:nav];
        
        [addressVC setSelectAddressBlock:^(NSString *address) {
            
        }];
        
    }else if (indexPath.row == 1) {
        YXMineServiceViewController *serviceVC = [[YXMineServiceViewController alloc] init];
        serviceVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"导航栏_002"] style:(UIBarButtonItemStyleDone) target:[YXDrawerViewController shareDrawer] action:@selector(backHome)];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:serviceVC];
        
        [[YXDrawerViewController shareDrawer] switchViewController:nav];
        
    }else if (indexPath.row == 2) {
   
    }else {
        YXMineSettingViewController *settingVC = [[YXMineSettingViewController alloc] init];
        settingVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"导航栏_002"] style:(UIBarButtonItemStyleDone) target:[YXDrawerViewController shareDrawer] action:@selector(backHome)];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:settingVC];
        
        [[YXDrawerViewController shareDrawer] switchViewController:nav];
        
    }

    
}


@end
